package br.com.easyrhour3x.main.relatorio;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;

import br.com.easyrhour3x.controller.SolicitacaoController;
import br.com.easyrhour3x.main.ConfigUIPadrao;
import br.com.easyrhour3x.main.SolicitacaoTableModel;
import br.com.easyrhour3x.main.UIMain;
import br.com.easyrhour3x.util.CellRenderer;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class UIAtendimento extends ConfigUIPadrao {

	private static final long serialVersionUID = 1L;
	private JButton btnProcurar = new JButton("");
	private SolicitacaoController solicitacaoController = new SolicitacaoController();
	private SolicitacaoTableModel model = new SolicitacaoTableModel(null, 7);
	private JComboBox comboBoxState = new JComboBox();
	private JPanel contentPane;
	private JTextField textID;
	private JTextField textProjeto;
	private JLabel lblProjeto;
	private JTextField textDisciplina;
	private JLabel lblDisciplina;
	private JLabel lblState;
	private JTable table;

	private void limparCampos() {
		textID.setText("");
		textDisciplina.setText("");
		textProjeto.setText("");
		comboBoxState.setSelectedItem("Todos");
	}

	private void procurar() {
		model.deleteAndGo(solicitacaoController.procurarAtendimento());
	}

	public UIAtendimento() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				sair();
			}
		});
		setTitle("Relat\u00F3rio de Atendimento");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(658, 400);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"),
				"Pesquisar", TitledBorder.LEFT, TitledBorder.TOP, null, null));

		table = new JTable();
		table.setFont(new Font("Tahoma", Font.PLAIN, 14));
		table.setAutoCreateRowSorter(true);
		table.getTableHeader().setReorderingAllowed(false); // Trava a coluna
		table.setDefaultRenderer(Object.class, new CellRenderer()); // Centralizar
																	// linhas da
																	// JTable
		((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
				.setHorizontalAlignment(SwingConstants.CENTER);
		JScrollPane scroll = new JScrollPane(table);
		
		table.setModel(model);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Op\u00E7\u00F5es", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Procurar", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(gl_contentPane
				.createParallelGroup(Alignment.LEADING)
				.addComponent(scroll, GroupLayout.DEFAULT_SIZE, 632, Short.MAX_VALUE)
				.addGroup(
						gl_contentPane
								.createSequentialGroup()
								.addComponent(panel, GroupLayout.PREFERRED_SIZE, 463,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										gl_contentPane
												.createParallelGroup(Alignment.LEADING, false)
												.addComponent(panel_1,
														GroupLayout.PREFERRED_SIZE, 163,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(panel_2,
														GroupLayout.PREFERRED_SIZE, 163,
														GroupLayout.PREFERRED_SIZE))));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.TRAILING, false)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				panel_2,
																				0,
																				0,
																				Short.MAX_VALUE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				panel_1,
																				GroupLayout.PREFERRED_SIZE,
																				70,
																				GroupLayout.PREFERRED_SIZE))
														.addComponent(panel,
																GroupLayout.PREFERRED_SIZE,
																134,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(scroll, GroupLayout.DEFAULT_SIZE, 207,
												Short.MAX_VALUE)));
		btnProcurar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

			}
		});

		btnProcurar.setIcon(new ImageIcon(UIAtendimento.class
				.getResource("/br/com/easyrhour3x/imagens/procurar.png")));

		ButtonGroup buttonGroup = new ButtonGroup();

		JRadioButton rdbtnTodos = new JRadioButton("Todos");
		rdbtnTodos.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				btnProcurar.setEnabled(false);
				procurar();
			}
		});
		buttonGroup.add(rdbtnTodos);
		rdbtnTodos.setFont(new Font("Tahoma", Font.BOLD, 12));

		JRadioButton rdbtnParcial = new JRadioButton("Parcial");
		rdbtnParcial.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				btnProcurar.setEnabled(true);
			}
		});
		buttonGroup.add(rdbtnParcial);
		rdbtnParcial.setSelected(true);
		rdbtnParcial.setFont(new Font("Tahoma", Font.BOLD, 12));
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel_2.createSequentialGroup().addContainerGap()
								.addComponent(rdbtnTodos)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(rdbtnParcial)
								.addContainerGap(33, Short.MAX_VALUE)));
		gl_panel_2.setVerticalGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel_2
								.createSequentialGroup()
								.addGroup(
										gl_panel_2.createParallelGroup(Alignment.BASELINE)
												.addComponent(rdbtnTodos)
												.addComponent(rdbtnParcial))
								.addContainerGap(17, Short.MAX_VALUE)));
		panel_2.setLayout(gl_panel_2);

		JButton btnLimpar = new JButton("");
		btnLimpar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				limparCampos();
			}
		});
		btnLimpar.setToolTipText("Limpar Campos");
		btnLimpar.setIcon(new ImageIcon(UIAtendimento.class
				.getResource("/br/com/easyrhour3x/imagens/novo.png")));

		JButton btnRelatorio = new JButton("");
		btnRelatorio.setToolTipText("Gerar Relat\u00F3rio");
		btnRelatorio.setIcon(new ImageIcon(UIAtendimento.class
				.getResource("/br/com/easyrhour3x/imagens/relatorio.png")));
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel_1
								.createSequentialGroup()
								.addComponent(btnProcurar, GroupLayout.PREFERRED_SIZE, 42,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnLimpar, GroupLayout.PREFERRED_SIZE, 46,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnRelatorio, GroupLayout.PREFERRED_SIZE, 45,
										Short.MAX_VALUE).addContainerGap()));
		gl_panel_1.setVerticalGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
				.addGroup(
						gl_panel_1
								.createSequentialGroup()
								.addGroup(
										gl_panel_1
												.createParallelGroup(Alignment.TRAILING)
												.addComponent(btnRelatorio, Alignment.LEADING,
														GroupLayout.DEFAULT_SIZE, 36,
														Short.MAX_VALUE)
												.addComponent(btnLimpar,
														GroupLayout.DEFAULT_SIZE, 36,
														Short.MAX_VALUE)
												.addComponent(btnProcurar,
														GroupLayout.DEFAULT_SIZE, 36,
														Short.MAX_VALUE)).addContainerGap()));
		panel_1.setLayout(gl_panel_1);

		JLabel lblId = new JLabel("ID");
		lblId.setFont(new Font("Tahoma", Font.BOLD, 12));

		textID = new JTextField();
		textID.setFont(new Font("Tahoma", Font.PLAIN, 14));
		textID.setColumns(10);

		textProjeto = new JTextField();
		textProjeto.setFont(new Font("Tahoma", Font.PLAIN, 14));
		textProjeto.setColumns(10);

		lblProjeto = new JLabel("Projeto");
		lblProjeto.setFont(new Font("Tahoma", Font.BOLD, 12));

		textDisciplina = new JTextField();
		textDisciplina.setFont(new Font("Tahoma", Font.PLAIN, 14));
		textDisciplina.setColumns(10);

		lblDisciplina = new JLabel("Disciplina");
		lblDisciplina.setFont(new Font("Tahoma", Font.BOLD, 12));

		lblState = new JLabel("State");
		lblState.setFont(new Font("Tahoma", Font.BOLD, 12));

		comboBoxState.setModel(new DefaultComboBoxModel(new String[] { "Todos", "Solicitado",
				"Aguardando_Marca\u00E7\u00E3o", "Aguardando_Exclus\u00E3o",
				"Aguardando_Reintegra\u00E7\u00E3o" }));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGroup(
										gl_panel.createParallelGroup(Alignment.LEADING)
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.LEADING,
																				false)
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addGroup(
																										gl_panel.createParallelGroup(
																												Alignment.TRAILING,
																												false)
																												.addComponent(
																														textID)
																												.addComponent(
																														comboBoxState,
																														0,
																														144,
																														Short.MAX_VALUE))
																								.addPreferredGap(
																										ComponentPlacement.RELATED)
																								.addComponent(
																										textProjeto,
																										GroupLayout.PREFERRED_SIZE,
																										138,
																										GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										ComponentPlacement.RELATED))
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addGap(54)
																								.addComponent(
																										lblId)
																								.addPreferredGap(
																										ComponentPlacement.RELATED,
																										GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE)
																								.addComponent(
																										lblProjeto,
																										GroupLayout.PREFERRED_SIZE,
																										58,
																										GroupLayout.PREFERRED_SIZE)
																								.addGap(36)))
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.TRAILING)
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addGap(4)
																								.addComponent(
																										textDisciplina,
																										GroupLayout.DEFAULT_SIZE,
																										149,
																										Short.MAX_VALUE))
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addPreferredGap(
																										ComponentPlacement.RELATED)
																								.addComponent(
																										lblDisciplina,
																										GroupLayout.PREFERRED_SIZE,
																										58,
																										GroupLayout.PREFERRED_SIZE)
																								.addGap(41))))
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(44)
																.addComponent(lblState)))
								.addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addGroup(
								gl_panel.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblId)
										.addComponent(lblDisciplina,
												GroupLayout.PREFERRED_SIZE, 15,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(lblProjeto, GroupLayout.PREFERRED_SIZE,
												15, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(
								gl_panel.createParallelGroup(Alignment.BASELINE)
										.addComponent(textID, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(textProjeto, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(textDisciplina,
												GroupLayout.PREFERRED_SIZE, 23,
												GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(lblState, GroupLayout.PREFERRED_SIZE, 15,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(comboBoxState, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(20, Short.MAX_VALUE)));
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}

	@Override
	public void sair() {
		UIMain.uiAtendimento = null;
		dispose();
	}
}
